#!/bin/bash 
#author xlp
#20151218
dir_now=`pwd`
if [ $# -ne 4 ]
then
    echo "Usage xplot.sh 20151218 M151218_C00078 4 CATAS(miniOT,newOT) "
    exit 0
fi
obsdate=$1
ot2name=$2
ot2label=$3
ottype=$4
echo $obsdate $ot2name $ot2label $ottype 
pngfile=`echo $ot2name".png" | awk '{print($1)}'`
datafile=`echo $ot2name"_"$ottype".lc" | awk '{print($1)}'`
checkdatafile=`echo $ot2name"_CHECK.lc" | awk '{print($1)}'`
#dir_data=`pwd`
dir_data=`echo "/home/w60ccd/workspace/result/"$obsdate`
cd $dir_data
echo $pngfile $datafile $checkdatafile

firstdate=`head -1 $datafile | awk '{print($1)}'`

cat $datafile | awk '{if($19==a)print(($1-firstdate)*86400,$6,$7,$8,$9,$8-$15,$15)}' a=$ot2label firstdate=$firstdate | sort | uniq >list1
cat $checkdatafile | awk '{print(($1-firstdate)*86400,$8,$9)}' firstdate=$firstdate >list2

Ndatainlist1=`wc -l list1 | awk '{print($1)}'`
if [ $Ndatainlist1 -lt 1 ]
then
    echo "no data for your requist"
    exit 0
fi

gnuplot <<EOF

set term png
set output "$pngfile"
set title "$ot2name"
set size 1,1
set origin 0,0
set multiplot
set size 0.5,0.5
set origin 0,0.5

set xlabel "Time second" 
set ylabel "R-band magnitude" offset "2"
set yrange[] reverse
set grid
set xtics rotate by -45
set ytics 0.5
plot 'list1' u 1:4:5 t '60cm lc'

set size 0.5,0.5
set origin 0.5,0.5
set ylabel "R mag - USNO R2 mag"
plot 'list1' u 1:6 t 'DeltaMag'


set size 0.5,0.5
set origin 0,0
set xlabel "Time second"
set ylabel "R-band magnitude"
set ytics 0.2
set xtics rotate by -45
plot 'list2' u 1:2:3 w yerror t 'check star'

set size 0.5,0.5
set origin 0.5,0 
set xlabel "x pixel"
set ylabel "y pixel"
set ytics 0.5
set xtics 0.5
plot 'list1' u 2:3 w lp pt 1 ps 1
unset multiplot
reset
quit
EOF

display $pngfile &
cd $dir_now
